package com.example.ggpicturegg.Mysql_com;

import android.util.Log;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

import java.sql.DriverManager;
import java.sql.ResultSet;

public class DbOpenHelper {
    private static final String CLS = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://159.75.6.220:3306/imagedb";
    private static final String USER = "luzn";
    private static final String PWD = "123456";

    public static Connection conn;    //连接对象
    public static Statement stmt;       //命令集
    public static PreparedStatement pStmt;   //预编译集
    public static ResultSet rs;     //结果集

    // 取得连接的方法
    public static void getConnection(){
        try {
            Class.forName(CLS);
            conn = (Connection) DriverManager.getConnection(URL,USER,PWD);
        }catch(Exception e){
            Log.d("asdada_Connection", " -------------------------------------- error");
            e.printStackTrace();
        }
    }

    //关闭数据库
    public static void clossAll(){
        try{
            // 从小对象到大对象开始关闭
            if(rs!=null){
                rs.close();
                rs=null;
            }
            if(stmt!=null){
                stmt.close();
                stmt=null;
            }
            if(pStmt!=null){
                pStmt.close();
                pStmt=null;
            }
            if(conn!=null){
                conn.close();
                conn=null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
